<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dttx123.gitee.io","root":"/blog/","scheme":"Gemini","version":"8.0.0-rc.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="节点操作节点查找孩子节点123456&#x2F;&#x2F;childNodes:获取所有的孩子节点（包括了元素节点和其他很多类型的节点）&#x2F;&#x2F;children:获取所有的子元素（用途很广泛）&#x2F;&#x2F;firstChild &#x2F;&#x2F;第一个子节点&#x2F;&#x2F;firstElementChild &#x2F;&#x2F;第一个子元素&#x2F;&#x2F;lastChild &#x2F;&#x2F;最后一个节点&#x2F;&#x2F;lastElementChild &#x2F;&#x2F;最后一个子元素  兄弟节点1234&#x2F;&#x2F;1. nex">
<meta property="og:type" content="article">
<meta property="og:title" content="webAPI03">
<meta property="og:url" content="http://dttx123.gitee.io/2016/08/07/webAPI/03/index.html">
<meta property="og:site_name" content="Starry Night">
<meta property="og:description" content="节点操作节点查找孩子节点123456&#x2F;&#x2F;childNodes:获取所有的孩子节点（包括了元素节点和其他很多类型的节点）&#x2F;&#x2F;children:获取所有的子元素（用途很广泛）&#x2F;&#x2F;firstChild &#x2F;&#x2F;第一个子节点&#x2F;&#x2F;firstElementChild &#x2F;&#x2F;第一个子元素&#x2F;&#x2F;lastChild &#x2F;&#x2F;最后一个节点&#x2F;&#x2F;lastElementChild &#x2F;&#x2F;最后一个子元素  兄弟节点1234&#x2F;&#x2F;1. nex">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2016-08-06T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-13T03:05:00.224Z">
<meta property="article:author" content="大春哥">
<meta property="article:tag" content="dom操作">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dttx123.gitee.io/2016/08/07/webAPI/03/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>webAPI03 | Starry Night</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Starry Night</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">这就朕为你打下的江山</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dttx123.gitee.io/2016/08/07/webAPI/03/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="大春哥">
      <meta itemprop="description" content="IT">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Starry Night">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webAPI03
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-08-07 00:00:00" itemprop="dateCreated datePublished" datetime="2016-08-07T00:00:00+08:00">2016-08-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-13 11:05:00" itemprop="dateModified" datetime="2020-05-13T11:05:00+08:00">2020-05-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/dom%E6%93%8D%E4%BD%9C/" itemprop="url" rel="index"><span itemprop="name">dom操作</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="节点操作"><a href="#节点操作" class="headerlink" title="节点操作"></a>节点操作</h1><h2 id="节点查找"><a href="#节点查找" class="headerlink" title="节点查找"></a>节点查找</h2><h3 id="孩子节点"><a href="#孩子节点" class="headerlink" title="孩子节点"></a>孩子节点</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//childNodes:获取所有的孩子节点（包括了元素节点和其他很多类型的节点）</span></span><br><span class="line"><span class="comment">//children:获取所有的子元素（用途很广泛）</span></span><br><span class="line"><span class="comment">//firstChild //第一个子节点</span></span><br><span class="line"><span class="comment">//firstElementChild //第一个子元素</span></span><br><span class="line"><span class="comment">//lastChild //最后一个节点</span></span><br><span class="line"><span class="comment">//lastElementChild //最后一个子元素</span></span><br></pre></td></tr></table></figure>

<h3 id="兄弟节点"><a href="#兄弟节点" class="headerlink" title="兄弟节点"></a>兄弟节点</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. nextSibling:下一个兄弟节点</span></span><br><span class="line"><span class="comment">//2. nextElementSibling:下一个兄弟元素</span></span><br><span class="line"><span class="comment">//3. previousSibling//上一个兄弟节点</span></span><br><span class="line"><span class="comment">//4. previousElementSibling //上一个兄弟元素</span></span><br></pre></td></tr></table></figure>

<h3 id="父节点"><a href="#父节点" class="headerlink" title="父节点"></a>父节点</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. parentNode:父节点</span></span><br></pre></td></tr></table></figure>

<p>【案例：隔行变色】</p>
<p>【案例：表单校验】</p>
<h3 id="onkeydown与onkeyup事件"><a href="#onkeydown与onkeyup事件" class="headerlink" title="onkeydown与onkeyup事件"></a>onkeydown与onkeyup事件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> onkeydown: 当键盘按下时触发的事件</span><br><span class="line"><span class="number">2.</span> onkeyup: 键盘弹起时触发的事件</span><br><span class="line"></span><br><span class="line">注意：如果给文本框注册的是onkeydown事件，获取的value值是上一次的。</span><br></pre></td></tr></table></figure>



<h2 id="添加节点"><a href="#添加节点" class="headerlink" title="添加节点"></a>添加节点</h2><h3 id="appendChild"><a href="#appendChild" class="headerlink" title="appendChild"></a>appendChild</h3><p>语法：parent.appendChild(child)</p>
<p>parent: 调用者，父节点来调用</p>
<p>child:需要添加的那个孩子。</p>
<p>作用：把child添加到parent的孩子的最后面。</p>
<blockquote>
<p>如果添加的是页面中本来就存在的元素，是一个剪切的效果，原来的就不在了。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> demo = <span class="built_in">document</span>.getElementById(<span class="string">"demo"</span>);</span><br><span class="line"><span class="keyword">var</span> box = <span class="built_in">document</span>.getElementById(<span class="string">"box"</span>);</span><br><span class="line">box.appendChild(demo);</span><br></pre></td></tr></table></figure>

<h3 id="insertBefore"><a href="#insertBefore" class="headerlink" title="insertBefore"></a>insertBefore</h3><p>语法：parent.insertBefore(child, refChild);</p>
<p>parent:必须要父节点来调用</p>
<p>child：需要添加的那个节点</p>
<p>refChild:添加到哪一个节点的前面。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ul = <span class="built_in">document</span>.getElementById(<span class="string">"list"</span>);</span><br><span class="line"><span class="keyword">var</span> li = <span class="built_in">document</span>.createElement(<span class="string">"li"</span>);</span><br><span class="line">li.innerHTML = <span class="string">"骥骥"</span>;</span><br><span class="line"><span class="comment">//就是添加到子节点的最前面。</span></span><br><span class="line">ul.insertBefore(li, ul.children[<span class="number">0</span>]);</span><br></pre></td></tr></table></figure>



<h2 id="克隆节点"><a href="#克隆节点" class="headerlink" title="克隆节点"></a>克隆节点</h2><p>语法：var newNode = node.cloneNode(deep)</p>
<p>功能：在内存中克隆一份节点</p>
<p>参数：deep</p>
<ul>
<li>false：默认值：是浅复制，只会复制标签，节点本身，不会复制节点的孩子。</li>
<li>true:深度复制，会复制标签，还会复制标签的所有内容 — <strong>常用</strong></li>
</ul>
<blockquote>
<ol>
<li>克隆出来的节点跟原来的节点没有关系了，修改了也不会相互影响。</li>
<li>如果克隆的节点带了id，我们需要给id重新设置一个值，不让id冲突</li>
</ol>
</blockquote>
<h2 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h2><p>语法：parent.removeChild(child);</p>
<p>功能：由父盒子调用，删除里面的一个子元素。</p>
<p>【案例：祝愿墙】 —  ondblclick：双击的时候触发</p>
<h2 id="创建节点（3种方式）"><a href="#创建节点（3种方式）" class="headerlink" title="创建节点（3种方式）"></a>创建节点（3种方式）</h2><h3 id="document-write（基本不用）"><a href="#document-write（基本不用）" class="headerlink" title="document.write（基本不用）"></a>document.write（基本不用）</h3><p>可以生成新的节点，但是不推荐使用。<strong><em>如果页面已经加载完成了，你还是用document.write写内容的话，会把之前的页面给覆盖掉</em></strong> </p>
<p>原理：页面从上往下加载的时候，会开启一个文档流，当页面加载完，文档流就会关闭。</p>
<p>document.write的本意就是在文档流上写入内容。如果页面没加载完成，文档流还是开着的，document.write直接在这个文档流上写东西</p>
<p>如果页面加载完成了，还是用document.write写东西，会重新开启一个新的文档流，往新的文档流上写东西，旧的文档流就被新的文档流覆盖了。</p>
<h3 id="innerHTML"><a href="#innerHTML" class="headerlink" title="innerHTML"></a>innerHTML</h3><p>innerHTML也可以创建节点</p>
<blockquote>
<p>innerHTML创建节点的时候有一个特点，如果原来有内容的话，使用innerHTML会把原先的内容给干掉。</p>
<p>慎用：很容易出现性能效率问题。</p>
</blockquote>
<h3 id="createElement"><a href="#createElement" class="headerlink" title="createElement"></a>createElement</h3><p>语法：var element = document.createElement(“tagName”);</p>
<p>作用：在内存里面创建了一个节点</p>
<p>返回：一个元素。 用途非常的广泛。</p>
<p>【案例：微博发布】</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/dom%E6%93%8D%E4%BD%9C/" rel="tag"># dom操作</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2016/07/03/webAPI/02/" rel="prev" title="webAPI 02">
      <i class="fa fa-chevron-left"></i> webAPI 02
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2016/08/07/webAPI/04/" rel="next" title="webAPI04">
      webAPI04 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#节点操作"><span class="nav-text">节点操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#节点查找"><span class="nav-text">节点查找</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#孩子节点"><span class="nav-text">孩子节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#兄弟节点"><span class="nav-text">兄弟节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#父节点"><span class="nav-text">父节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#onkeydown与onkeyup事件"><span class="nav-text">onkeydown与onkeyup事件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加节点"><span class="nav-text">添加节点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#appendChild"><span class="nav-text">appendChild</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#insertBefore"><span class="nav-text">insertBefore</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#克隆节点"><span class="nav-text">克隆节点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除节点"><span class="nav-text">删除节点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建节点（3种方式）"><span class="nav-text">创建节点（3种方式）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#document-write（基本不用）"><span class="nav-text">document.write（基本不用）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#innerHTML"><span class="nav-text">innerHTML</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#createElement"><span class="nav-text">createElement</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大春哥"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大春哥</p>
  <div class="site-description" itemprop="description">IT</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大春哥</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

</body>
</html>
